###########################################################################
#							_              
# ~Q~				       (_)             
#			      ___ _   _ _ _____      __
#			     / __| | | | / __\ \ /\ / /
#			    | (__| |_| | \__ \\ V  V / 
#			     \___|\__,_|_|___/ \_/\_/  
#
# Copyright (C) 2008 - 8002, Cui Shaowei, <shaovie@gmail.com>, It's free.
# This is a general makefile template.
###########################################################################
#compiler and linker 
CC      			= gcc
AR					= ar
CXX     			= g++

BIN					= libnetdkit.so.1.0
MAKE    			= make

LIBS  				= -lpthread
LINKER				= g++
LIBDIR 				= -L./lib
LFLAGS				= -fPIC -shared -Wl

DEFINES     	   	= -D_REENTRANT -D__USE_POSIX -DNDK_LINUX

CCFLAGS				= $(DEFINES) -Wall -W -Wpointer-arith -pipe
ARFLAGS				= -cr
CXXFLAGS			= $(DEFINES) -Wall -W -Wpointer-arith -pipe

OBJECTDIR  			= ./.obj/
INCLUDEDIR   		= -I./include -I./src -I./logger

DEBUGBINDIR			= ./bin
RELEASEBINDIR 		= ./bin
DEBUGOPTIMFLAGS 	= -g3 -O2 -DNDK_DEBUG -DNDK_LOG_DEBUG#-DNDK_TRACE
RELEASEOPTIMFLAGS	= -O2

ifneq ($(debug), 0)
	BINDIR 			= $(DEBUGBINDIR)
	OPTIMFLAGS		= $(DEBUGOPTIMFLAGS)
else
	BINDIR 			= $(RELEASEBINDIR)
	OPTIMFLAGS		= $(RELEASEOPTIMFLAGS)
endif

CXXFLAGS 	 		+= $(OPTIMFLAGS)
CCFLAGS  			+= $(OPTIMFLAGS)
LFLAGS				+= $(OPTIMFLAGS)

VPATH  				= ./src:./logger

# ********************** target **************************
TARGET  		= $(BINDIR)/$(BIN)
# ********************************************************

CXXFILES 		= \
				  Acceptor.cpp  					\
				  AsynchAcceptor.cpp				\
				  AsynchIO.cpp						\
				  AsynchIOImpl.cpp					\
				  EpollAsynchIO.cpp					\
				  EpollProactor.cpp  				\
				  EpollReactor.cpp  				\
				  MultiReactors.cpp					\
				  InetAddr.cpp  					\
				  LogSvr.cpp  						\
				  Logger.cpp						\
				  LogManager.cpp					\
				  LogOutput.cpp						\
				  LogToFiles.cpp					\
				  LogToStdout.cpp					\
				  MessageQueue.cpp  				\
				  NDK.cpp  							\
				  Pipe.cpp  						\
				  Proactor.cpp						\
				  ProactorImpl.cpp					\
				  Reactor.cpp  						\
				  ReactorImpl.cpp  					\
				  ReactorToken.cpp  				\
				  RecursiveThreadMutex.cpp  		\
				  SockAcceptor.cpp  				\
				  SockConnector.cpp  				\
				  SvcHandler.cpp  					\
				  TaskBase.cpp  					\
				  Token.cpp  						\
				  Thread.cpp  						\
				  ThreadManager.cpp  				\
				  ThreadTimerSvc.cpp				\
				  TimerQueue.cpp  					\
				  TimeValue.cpp   					\
				  Trace.cpp  

CFILES 			= \
				  dictionary.c						\
				  iniparser.c				

OBJECTS  		= $(addprefix $(OBJECTDIR), $(notdir $(CXXFILES:%.cpp=%.o))) $(addprefix $(OBJECTDIR), $(notdir $(CFILES:%.c=%.o)))

all : checkdir $(TARGET)

$(TARGET): $(OBJECTS)
	$(LINKER) $(LFLAGS) -o $@ $(OBJECTS)
	cd $(BINDIR) ; \
		ln -fs $(BIN) libnetdkit.so
$(OBJECTDIR)%.o:%.cpp
	$(CXX) $(CXXFLAGS) -c $^ -o $@ $(INCLUDEDIR)
$(OBJECTDIR)%.o:%.c
	$(CXX) $(CXXFLAGS) -c $^ -o $@ $(INCLUDEDIR)

checkdir:
	@if ! [ -d "$(BINDIR)" ]; then \
		mkdir $(BINDIR) ; \
		fi
	@if ! [ -d "$(OBJECTDIR)" ]; then \
		mkdir $(OBJECTDIR); \
		fi 
clean:
	rm -f $(OBJECTS)
cleanall:clean 
	rm -f $(TARGET)
.PHONY:all clean cleanall checkdir
